<?php

$image = $_GET['image'];

$maxWidth = isset($_GET['width']) ? $_GET['width'] : 500;
$maxHeight = isset($_GET['height']) ? $_GET['height'] : 500;

$size = GetImageSize($image);
$width = $size[0];
$height = $size[1];

$xRatio = $maxWidth / $width;
$yRatio = $maxHeight / $height;

if(($width <= $maxWidth) && ($height <= $maxHeight)) {
	$tnWidth = $width;
	$tnHeight = $height;
}
else if(($xRatio * $height) < $maxHeight) {
	$tnHeight = ceil($xRatio * $height);
	$tnWidth = $maxWidth;
}
else {
	$tnWidth = ceil($yRatio * $width);
	$tnHeight = $maxHeight;
}

$src = ImageCreateFromJpeg($image);
$dst = ImageCreate($tnWidth, $tnHeight);

ImageCopyResized($dst, $src, 0, 0, 0, 0, $tnWidth, $tnHeight, $width, $height);

header("Content-type: image/jpeg");

ImageJpeg($dst, NULL, -1);

ImageDestroy($dst);
ImageDestroy($src);

?>